package com.gzsxy.esjy.service.file.controller;

import com.gzsxy.esjy.common.base.result.R;
import com.gzsxy.esjy.service.file.entity.FileResource;
import com.gzsxy.esjy.service.file.entity.dto.UploadDto;
import com.gzsxy.esjy.service.file.service.FileService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author xiaolong
 * @version 1.0
 * @description: 文件服务
 * @date 2021/12/21 14:35
 */
@CrossOrigin  //解决跨域
@RestController
@RefreshScope    //动态刷新配置文件-外部配置文件
@RequestMapping("/file")
public class FileController {

    @Autowired
    private FileService fileService;

    @ApiOperation("文件上传")
    @PostMapping("upload")
    public R upload(@ApiParam(value = "文件",required = true)
                        @RequestParam("file") MultipartFile file,
                    @ApiParam(value = "模块",required = false)
                        @RequestParam(value = "module",required = false)  String module){
        FileResource fileResource = fileService.upload(file);
        return R.ok().message("文件上传成功").data("data",fileResource);
    }

    @ApiOperation("文件上传-base64")
    @PostMapping("/uploadBase64")
    public R uploadBase64(@RequestBody UploadDto uploadDto) throws Exception {
        FileResource fileResource = fileService.uploadBase64(uploadDto);
        return R.ok().message("文件上传成功").data("data",fileResource);
    }


    @ApiOperation("删除文件")
    @DeleteMapping("delete")
    public R delete(@ApiParam(value = "文件索引",required = true)
                    @RequestParam("fileIndex") String fileIndex){
        fileService.delete(fileIndex);
        return R.ok().message("删除成功");
    }


    @Value("${config.info}")
    private String configInfo;

    @GetMapping("/configInfo")
    @ApiOperation("测试配置中心刷新")
    public String getConfigInfo() {
        return configInfo;
    }


}
